package com.rmy.queue.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.rmy.queue.common.QueueConst;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.rmy.common.annotation.Log;
import com.rmy.common.core.controller.BaseController;
import com.rmy.common.core.domain.AjaxResult;
import com.rmy.common.enums.BusinessType;
import com.rmy.queue.domain.QmsVoicConfig;
import com.rmy.queue.service.IQmsVoicConfigService;
import com.rmy.common.utils.poi.ExcelUtil;
import com.rmy.common.core.page.TableDataInfo;
import rmy.VoicUtils;
import rmy.domain.VoicCmd;

/**
 * 语音配置Controller
 *
 * @author rmy
 * @date 2022-10-12
 */
@RestController
@RequestMapping("/queue/voicconfig")
public class QmsVoicConfigController extends BaseController
{
    @Autowired
    private IQmsVoicConfigService qmsVoicConfigService;

    /**
     * 查询语音配置列表
     */
    @PreAuthorize("@ss.hasPermi('queue:voicconfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(QmsVoicConfig qmsVoicConfig)
    {
        startPage();
        List<QmsVoicConfig> list = qmsVoicConfigService.selectQmsVoicConfigList(qmsVoicConfig);
        return getDataTable(list);
    }

    /**
     * 导出语音配置列表
     */
    @PreAuthorize("@ss.hasPermi('queue:voicconfig:export')")
    @Log(title = "语音配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QmsVoicConfig qmsVoicConfig)
    {
        List<QmsVoicConfig> list = qmsVoicConfigService.selectQmsVoicConfigList(qmsVoicConfig);
        ExcelUtil<QmsVoicConfig> util = new ExcelUtil<QmsVoicConfig>(QmsVoicConfig.class);
        util.exportExcel(response, list, "语音配置数据");
    }

    /**
     * 获取语音配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('queue:voicconfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qmsVoicConfigService.selectQmsVoicConfigById(id));
    }

    /**
     * 新增语音配置
     */
    @PreAuthorize("@ss.hasPermi('queue:voicconfig:add')")
    @Log(title = "语音配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QmsVoicConfig qmsVoicConfig)
    {
        return toAjax(qmsVoicConfigService.insertQmsVoicConfig(qmsVoicConfig));
    }

    /**
     * 修改语音配置
     */
    @PreAuthorize("@ss.hasPermi('queue:voicconfig:edit')")
    @Log(title = "语音配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QmsVoicConfig qmsVoicConfig)
    {
        return toAjax(qmsVoicConfigService.updateQmsVoicConfig(qmsVoicConfig));
    }

    /**
     * 删除语音配置
     */
    @PreAuthorize("@ss.hasPermi('queue:voicconfig:remove')")
    @Log(title = "语音配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qmsVoicConfigService.deleteQmsVoicConfigByIds(ids));
    }

    /**
     * 发送语音测试
     */
    @PostMapping(value = "/test/{id}")
    public AjaxResult test(@PathVariable("id") Long id, @RequestParam String testContent)
    {
        QmsVoicConfig qmsVoicConfig = qmsVoicConfigService.selectQmsVoicConfigById(id);
        VoicCmd voicCmd = new VoicCmd();
        voicCmd.setSound_repeat(2);
        voicCmd.setSound_person(1);
        voicCmd.setSound_volume(10);
        voicCmd.setSound_speed(5);
        voicCmd.setSound_content(testContent);
        try {
            VoicUtils.sendVoic(qmsVoicConfig.getDeviceIp(), qmsVoicConfig.getDevicePort().intValue(), "{}", voicCmd);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }
}
